package com.killer.texreader.util;

import android.database.Cursor;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by killer on 2016/4/21.
 */
public class TxtDao {

    public static void insertTxtData(String fullPath){
        String sql = "select id from txt where full_path=?";
        Cursor c = Globals.util.getReadableDatabase().rawQuery(sql, new String[]{fullPath});
        if(!c.moveToFirst()){
            sql="insert into txt(full_path, now_pager, over_flag) values(?,1,0)";
            Globals.util.getWritableDatabase().execSQL(sql,new Object[]{fullPath});
        }
        if (!c.isClosed()){
            c.close();
        }

    }

    public static Map<String,Object> findTxtDataByFullPath(String fullPath){
        Map<String,Object> map = new HashMap<>();
        String sql = "select id, now_pager, over_flag from txt where full_path=?";
        Cursor c = Globals.util.getReadableDatabase().rawQuery(sql,new String[]{fullPath});
        c.moveToFirst();
        map.put("txtid",c.getInt(0));
        map.put("now_pager",c.getInt(1));
        map.put("overFlag",c.getInt(2));
        if (!c.isClosed()){
            c.close();
        }
        return map;

    }



    public static void updateTxtOverFlag(String fullPath) {
        String sql = "update txt set over_flag=1 where full_path=?";
        Globals.util.getWritableDatabase().execSQL(sql, new Object[]{fullPath});
    }

    public static void updateNowPage(int nowPager, String fullPath) {
        String sql = "update txt set now_pager=? where full_path=?";
        Globals.util.getWritableDatabase().execSQL(sql, new Object[]{nowPager, fullPath});
    }

}
